using System;

namespace armcontrolgui.RandomLogicArm
{
	[System.ComponentModel.ToolboxItem(true)]
	public class ArmFrontView : Gtk.DrawingArea
	{
		private RLArm rla_CurrentArm;
		public ArmFrontView (RLArm Arm)
		{
			// Insert initialization code here.
			this.rla_CurrentArm = Arm;
		}
		
		protected override bool OnButtonPressEvent (Gdk.EventButton ev)
		{
			// Insert button press handling code here.
			return base.OnButtonPressEvent (ev);
		}
		
		protected override bool OnExposeEvent (Gdk.EventExpose ev)
		{
			base.OnExposeEvent (ev);
			
			using(Cairo.Context CairoContext = Gdk.CairoHelper.Create(this.GdkWindow)) {
			
				Cairo.Rectangle Rect = new Cairo.Rectangle(4, 4, this.Allocation.Width - 8, this.Allocation.Height - 8);
				
				//White Background
				CairoContext.Rectangle(Rect);
				CairoContext.SetSourceRGB(1, 1, 1);
				CairoContext.FillPreserve();
				CairoContext.LineWidth = 1;
				CairoContext.SetSourceRGB(0.2, 0.2, 0.2);
				CairoContext.Stroke();
				
				//Lines
				CairoContext.LineWidth = 1;
				CairoContext.SetSourceRGB(0.5, 0.5, 0.5);
				for (double i = 0; i < Rect.Width; i += Rect.Width / 20) {
					CairoContext.MoveTo(i + Rect.X , Rect.Y);
					CairoContext.LineTo(i + Rect.X, Rect.Y + Rect.Height);
				}
				CairoContext.Stroke();
				
				CairoContext.SetSourceRGB(0.5, 0.5, 0.5);
				for (double i = 0; i < Rect.Height; i += Rect.Height / 20) {
					CairoContext.MoveTo(Rect.X , i + Rect.Y);
					CairoContext.LineTo(Rect.X + Rect.Width, i + Rect.Y);
				}
				CairoContext.Stroke();
				
				//Arm
				
				//Calc X Y
				
				double ShoulderLength = RLArm.BaseRadius;
				double ShoulderY = RLArm.BaseHeight;
				double CurrentAngle = this.rla_CurrentArm["Shoulder"].Position + RLArm.ShoulderNeutralAngle;
				double ElbowLength = Math.Cos(RLArm.Radians(CurrentAngle)) * RLArm.Humerus + ShoulderLength;
				double ElbowY = Math.Sin(RLArm.Radians(CurrentAngle)) * RLArm.Humerus + ShoulderY;
				CurrentAngle += this.rla_CurrentArm["Elbow"].Position + RLArm.ElbowNeutralAngle;
				double WristLength = Math.Cos(RLArm.Radians(CurrentAngle)) * RLArm.Ulna + ElbowLength;
				double WristY = Math.Sin(RLArm.Radians(CurrentAngle)) * RLArm.Ulna + ElbowY;
				CurrentAngle += this.rla_CurrentArm["Wrist"].Position + RLArm.WristNeutralAngle;
				double FingersLength = Math.Cos(RLArm.Radians(CurrentAngle)) * RLArm.Hand + WristLength;
				double FingersY = Math.Sin(RLArm.Radians(CurrentAngle)) * RLArm.Hand + WristY;
				
				CairoContext.Save();
				CairoContext.Translate(Rect.X + (Rect.Width / 2), Rect.Y + Rect.Height);
				CairoContext.Scale(0.9, -0.9);
				
				//Table Red dot
				CairoContext.SetSourceRGB(1,0,0);
				CairoContext.Arc(0,0, 5, 0, 2 * Math.PI);
				CairoContext.FillPreserve();
				CairoContext.SetSourceRGB(0.2, 0.2, 0.2);
				CairoContext.Stroke();
				
				//ConnectingLine
				CairoContext.MoveTo(0, 0);
				CairoContext.LineTo(0, RLArm.BaseHeight);
				CairoContext.SetSourceRGB(0.2, 0.2, 0.2);
				CairoContext.Stroke();
				
				//Base Red dot
				CairoContext.SetSourceRGB(1,0,0);
				CairoContext.Arc(0,RLArm.BaseHeight, 5, 0, 2 * Math.PI);
				CairoContext.FillPreserve();
				CairoContext.SetSourceRGB(0.2, 0.2, 0.2);
				CairoContext.Stroke();
				
				//ConnectingLine
				CairoContext.MoveTo(0, RLArm.BaseHeight);
				CairoContext.LineTo(ShoulderLength, ShoulderY);
				CairoContext.SetSourceRGB(0.2, 0.2, 0.2);
				CairoContext.Stroke();
				
				//Shoulder Green dot
				CairoContext.SetSourceRGB(0,1,0);
				CairoContext.Arc(ShoulderLength,ShoulderY, 5, 0, 2 * Math.PI);
				CairoContext.FillPreserve();
				CairoContext.SetSourceRGB(0.2, 0.2, 0.2);
				CairoContext.Stroke();
				
				//ConnectingLine
				CairoContext.MoveTo(ShoulderLength, ShoulderY);
				CairoContext.LineTo(ElbowLength, ElbowY);
				CairoContext.SetSourceRGB(0.2, 0.2, 0.2);
				CairoContext.Stroke();
				
				//Elbow Yellow dot
				CairoContext.SetSourceRGB(1,1,0);
				CairoContext.Arc(ElbowLength, ElbowY, 5, 0, 2 * Math.PI);
				CairoContext.FillPreserve();
				CairoContext.SetSourceRGB(0.2, 0.2, 0.2);
				CairoContext.Stroke();
				
				//ConnectingLine
				CairoContext.MoveTo(ElbowLength, ElbowY);
				CairoContext.LineTo(WristLength, WristY);
				CairoContext.SetSourceRGB(0.2, 0.2, 0.2);
				CairoContext.Stroke();
				
				//Wrist Blue dot
				CairoContext.SetSourceRGB(0,0,1);
				CairoContext.Arc(WristLength, WristY, 5, 0, 2 * Math.PI);
				CairoContext.FillPreserve();
				CairoContext.SetSourceRGB(0.2, 0.2, 0.2);
				CairoContext.Stroke();
				
				//ConnectingLine
				CairoContext.MoveTo(WristLength, WristY);
				CairoContext.LineTo(FingersLength, FingersY);
				CairoContext.SetSourceRGB(0.2, 0.2, 0.2);
				CairoContext.Stroke();
				
				//Finger Pink dot
				CairoContext.SetSourceRGB(1,0,1);
				CairoContext.Arc(FingersLength, FingersY, 5, 0, 2 * Math.PI);
				CairoContext.FillPreserve();
				CairoContext.SetSourceRGB(0.2, 0.2, 0.2);
				CairoContext.Stroke();
				
			}
			return true;
		}
		protected override void OnSizeAllocated (Gdk.Rectangle allocation)
		{
			base.OnSizeAllocated (allocation);
			// Insert layout code here.
		}
		protected override void OnSizeRequested (ref Gtk.Requisition requisition)
		{
			// Calculate desired size here.
			requisition.Height = 200;
			requisition.Width = 200;
		}
	}
}

